package org.kealinghornets.nxtdroid;

import org.kealinghornets.nxtdroid.NXT.LightSensor;
import org.kealinghornets.nxtdroid.NXT.NXTThread;
import org.kealinghornets.nxtdroid.NXT.TouchSensor;

/**
 * Created by jychuah on 11/3/13.
 */
public class MyNXTThread extends NXTThread {

    int state = 1;
    LightSensor eyeball;

    public MyNXTThread() {
        setNXTThreadName("Bridge Crosser");
    }

    public void run() {
        while (state != -1) {
            switch (state) {
                case 1:
                    do_state_1();
                    break;
                case 2:
                    do_state_2();
                    break;
                case 3:
                    do_state_3();
                    break;
                case 4:
                    do_state_4();
                    break;
                case 5:
                    do_state_5();
                    break;
                case 6:
                    do_state_6();
                    break;
                case 7:
                    do_state_7();
                    break;
                case 8:
                    do_state_8();
                    break;
                case 9:
                    do_state_9();
                    break;
                case 10:
                    do_state_10();
                    break;
                default:
                    state = -1;
                    break;
            }
        }
    }

    public void do_state_1() {
        LightSensor eyeball = getActiveLightSensor(PORT_1);
        int lightValue = 0;
        log("I'm in state 1");
        runMotor(PORT_A, 75);
        runMotor(PORT_B, -75);
        wait(218);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        wait(2000);
        lightValue = eyeball.getLightValue();
        if (lightValue < 260) {
            log("Light value less than 260, going to state 3");
            state = 3;
        } else {
            log("Light value greater than 260, going to state 2");
            state = 2;
        }

    }

    public void do_state_2() {
        log("I'm in state 2");
        wait(1000);
        runMotor(PORT_A, 75);
        runMotor(PORT_B,75);
        wait(1050);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        state = 4;
    }
    public void do_state_3() {
        log("I'm in state 3");
        runMotor(PORT_A, 75);
        runMotor(PORT_B, -75);
        wait(670);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        state = 2;
    }
    public void do_state_4() {
        wait(1000);
        LightSensor eyeball = getActiveLightSensor(PORT_1);
        int lightValue = 0;
        log("I'm in state 4");
        runMotor(PORT_A, 75);
        runMotor(PORT_B, -75);
        wait(196);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        wait(1600);
        lightValue = eyeball.getLightValue();
        if (lightValue < 260) {
            state = 5;
        } else {
            state = 6;
        }
    }
    public void do_state_5() {
        log("I'm in state 5");
        runMotor(PORT_A, 75);
        runMotor(PORT_B, -75);
        wait(840);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        state = 6;
    }
        public void do_state_6() {
            wait(1000);
            log("I'm in state 6");
            runMotor(PORT_A, 75);
            runMotor(PORT_B, 75);
            wait(2250);
            brakeMotor(PORT_A);
            brakeMotor(PORT_B);
            state=7;
        }
    public void do_state_7() {
        log("I'm in state 7");
        wait(1000);
        runMotor(PORT_A, 75);
        runMotor(PORT_B, -75);
        wait(159);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        log ("going to state 8 now");
        state=8;
    }
    public void do_state_8() {
        LightSensor eyeball = getActiveLightSensor(PORT_1);
        int lightValue = 0;
        runMotor(PORT_A, 75);
        runMotor(PORT_B, 75);
        wait(100);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        wait(1000);
        lightValue = eyeball.getLightValue();
        if (lightValue > 430) {
            log("found bridge going across");
            state = 9;
        }
        if (lightValue<240) {
            log("found edge turning around");
            state=10;
        }
        if (lightValue >= 240 && lightValue < 430) {
            log("on table going again");
            state = 8;
        }


    }
    public void do_state_9() {
        runMotor(PORT_A, 90);
        runMotor(PORT_B, 90);
        wait(2000);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        state=-1;
    }
    public void do_state_10() {
        runMotor(PORT_A, -75);
        runMotor(PORT_B, -75);
        wait(200);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        wait(1000);
        runMotor(PORT_A, -75);
        runMotor(PORT_B, 75);
        wait(690);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);
        runMotor(PORT_A);
        runMotor(PORT_B);
        wait(2690);
        brakeMotor(PORT_A);
        brakeMotor(PORT_B);

        state=-1;
    }
      }




